package cmgine.com.testqq.TabBar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMGroupManager;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;

import cmgine.com.testqq.R;

public class ChatActivity extends AppCompatActivity {

    private EditText messageEditText;
    private Button sendBtn;
    private EMConversation conversation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        messageEditText = (EditText)findViewById(R.id.messageEditText);
        sendBtn = (Button)findViewById(R.id.sendBtn);

        EMChatManager.getInstance().login("cnbxw2","123456",new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        EMGroupManager.getInstance().loadAllGroups();
                        EMChatManager.getInstance().loadAllConversations();
                        Log.d("main", "登陆聊天服务器成功！");
                        try {
                            EMContactManager.getInstance().addContact("cnbxw1", "test");//需异步处理
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登陆聊天服务器失败！");
            }
        });


        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取到与聊天人的会话对象。参数username为聊天人的userid或者groupid，后文中的username皆是如此
                if(conversation == null) {
                    conversation = EMChatManager.getInstance().getConversation("cnbxw1");
                }
                //创建一条文本消息
                EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
                //如果是群聊，设置chattype,默认是单聊
                message.setChatType(EMMessage.ChatType.Chat); //这个地方
                String msg = messageEditText.getText().toString();
                Log.d("main", msg);
                //设置消息body
                TextMessageBody txtBody = new TextMessageBody(msg);
                message.addBody(txtBody);
                //设置接收人
                message.setReceipt("cnbxw1");
                //把消息加入到此会话对象中
                conversation.addMessage(message);
                //发送消息
                EMChatManager.getInstance().sendMessage(message, new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        Log.d("main", "发送成功了。。。。。。");
                    }

                    @Override
                    public void onError(int i, String s) {
                        Log.d("main", "发送失败了。。。。。。");
                    }

                    @Override
                    public void onProgress(int i, String s) {

                    }
                });
            }
        });
    }
}
